package com.heima.crm.aop;

import com.heima.crm.annotation.AddNotice;
import com.heima.crm.common.result.Result;
import com.heima.crm.common.utils.BaseContext;
import com.heima.crm.enumeration.OperationType;
import com.heima.crm.mapper.CrUserMapper;
import com.heima.crm.mapper.SysNoticeMapper;
import com.heima.crm.pojos.entity.SysNotice;
import com.heima.crm.pojos.entity.TbUser;
import com.heima.crm.pojos.vos.NoticeVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Random;

@Aspect
@Component
@Slf4j
public class AddNoticeAspect {
    @Autowired
    private CrUserMapper crUserMapper;
    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @Around("execution(* com.heima.crm.service.impl.*.*(..)) && @annotation(com.heima.crm.annotation.AddNotice)")
    public Object autoFill(ProceedingJoinPoint jp) throws Throwable {
        log.info("添加通知");
        //获取mapper方法的参数,即要进行填充的对象
        Object[] args = jp.getArgs();
        if (args==null || args.length==0){
            return null;
        }
        Object entity = args[0];

        //分添加还是修改,获取注解中的value值
        Signature signature = jp.getSignature();//签名
        MethodSignature methodSignature=(MethodSignature)signature;//方法
        AddNotice addNotice = methodSignature.getMethod().getAnnotation(AddNotice.class);//注解
        OperationType operationType = addNotice.value();//注解值,操作类型

        //根据不同操作类型,不同增强方法来反射赋值
        //新增,四个


        SysNotice sysNotice = new SysNotice();
        if (OperationType.ACTIVITY.equals(operationType)){
            //获取方法
            try {
                Method getCreateTime = entity.getClass().getDeclaredMethod("getCreateTime");
                Method getCreateUser = entity.getClass().getDeclaredMethod("getCreateUser");

                //LocalDateTime createTime = (LocalDateTime) getCreateTime.invoke(entity);
                //Long createUserId= (Long) getCreateUser.invoke(entity);
                Long createUserId = BaseContext.getCurrentId();
                TbUser tbUser = crUserMapper.selectById(createUserId);

                sysNotice.setNoticeContent("您好，"+tbUser.getUserName()+"提交了活动HD"+123456+"审核申请需要处理！");
                sysNotice.setCreateTime(LocalDateTime.now());
                sysNotice.setNoticeUserId(createUserId);
                sysNotice.setNoticeUserName(tbUser.getUserName());
                sysNotice.setNoticeType("1");
                sysNotice.setNoticeTitle("活动通知"+tbUser.getUserName());
                sysNotice.setStatus("0");
                sysNotice.setLookUp(4);
            } catch (NoSuchMethodException e) {
            /*    e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();*/
            }
            //修改,两个
        }else if (OperationType.CONTRACT.equals(operationType)){
            try {
                Method getCreateTime = entity.getClass().getDeclaredMethod("getCreateTime");
                Method getCreateUser = entity.getClass().getDeclaredMethod("getCreateId");

                //LocalDateTime createTime = (LocalDateTime) getCreateTime.invoke(entity);
                //Long createUserId= (Long) getCreateUser.invoke(entity);
                Long  createUserId= BaseContext.getCurrentId();
                TbUser tbUser = crUserMapper.selectById(createUserId);

                sysNotice.setNoticeContent("您好，"+tbUser.getUserName()+"提交了合同HT"+123456+"审核申请需要处理！");
                sysNotice.setCreateTime(LocalDateTime.now());
                sysNotice.setNoticeUserId(createUserId);
                sysNotice.setNoticeUserName(tbUser.getUserName());
                sysNotice.setNoticeType("2");
                sysNotice.setNoticeTitle("合同通知"+tbUser.getUserName());
                sysNotice.setStatus("0");
                sysNotice.setLookUp(3);
            } catch (NoSuchMethodException e) {
            /*    e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();*/
            }
        }
        Result result = (Result) jp.proceed();
        if (result.getCode()==1){
            sysNoticeMapper.insert(sysNotice);
        }
        return Result.success();
    }
}
